//
//  Vector2D.m
//  Coffee
//
//  Created by sabhanayagam b on 5/27/11.
//  Copyright 2011 Cognizant. All rights reserved.
//

#import "Vector2D.h"


@implementation Vector2D
@synthesize vx,vy;
@synthesize p0,p1;

-(void) dealloc
{
	
	[super dealloc];
}
-(void) implementation
{
	//TO DO
	
}

+(Vector2D*) createVector
{
	return [[Vector2D alloc] initWithComponentX:0.0 Y:0.0];
}
+(Vector2D*) createVectorWithComponentX:(float)x Y:(float)y
{
	return [[Vector2D alloc] initWithComponentX:x Y:y];
}

//Initial Vector using Components
-(id) initWithComponentX:(float)x Y:(float)y
{
	if ((self = [super init])) {
		//Assin vector components
		self.vx = x;
		self.vy = y;
		[self implementation];
	}
	
    return self;
}
//Initialize Vector using two points
-(id) initwithPointA:(CGPoint) point0 PointB:(CGPoint)point1
{
	if ((self = [super init])) {
		//Set Points
		self.p0 = point0;
		self.p1 = point1;
		[self initWithComponentX:(p1.x - p0.x) Y:(p1.y - p0.y)];
	}
	return self;
}
//Vector DOT Product
-(float) dotProduct:(Vector2D*)vect2
{
	return ((vx * vect2.vx) + (vy * vect2.vy)); 
	
}

/**
 * Calculate and return this vector's right normal.
 */
-(Vector2D*) rightNormal
{
	Vector2D* rightnormal = [[[Vector2D alloc]initWithComponentX: -vy Y: vx]autorelease];
	return rightnormal;
}

/**
 * Calculate and return this vector's left normal.
 */
-(Vector2D*) leftNormal
{
	Vector2D* leftnormal = [[[Vector2D alloc]initWithComponentX: -vy Y: vx]autorelease];
	return leftnormal;
}
/**
 * Creates a unit vector.
 */
-(Vector2D*) normalize
{
	Vector2D* vector = [self clone];
	//Length of the Vector
	float length = sqrt((vector.vx * vector.vx) + (vector.vy * vector.vy));
	
	if(vector.vx != 0) vector.vx /= length;
	if(vector.vy != 0) vector.vy /= length;
	
	return vector;
}
/**
 * Clones this object.
 */
-(Vector2D*) clone
{
	Vector2D* thisCopy = [[[Vector2D alloc]initWithComponentX: vx Y: vy]autorelease];

	return thisCopy;
}
@end
